#!/usr/bin/python
# coding=utf-8
import random, sys, logging, time, traceback
from datetime import datetime
from datetime import date
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.executors.pool import ProcessPoolExecutor

g_scheduler = None

g_times_4_zgh = 0
G_JOB_ID_4_ZGH = 'zgh'

g_times_4_ssh = 0
G_JOB_ID_4_SSH = 'ssh'


def run_zgh_job(p_job_id, p_ver):
    global g_times_4_zgh
    g_times_4_zgh += 1

    job = g_scheduler.get_job(job_id=p_job_id)

    # scheduler.print_jobs()  # 输出所有格式化的作业列表。
    print(f'zgh->times={g_times_4_zgh}, p_job_id={p_job_id}, p_ver={p_ver}, state={g_scheduler.state}', file=sys.stderr)


def run_ssh_job(p_job_id, p_ver):
    global g_times_4_ssh
    g_times_4_ssh += 1

    job = g_scheduler.get_job(job_id=p_job_id)

    # scheduler.print_jobs()  # 输出所有格式化的作业列表。
    print(f'ssh->times={g_times_4_ssh}, p_job_id={p_job_id}, p_ver={p_ver}, state={g_scheduler.state}', file=sys.stdout)


def main():
    global g_scheduler

    exeutors = {
        "default": ProcessPoolExecutor(max_workers=10)
    }
    g_scheduler = BackgroundScheduler(exeutors=exeutors)
    zgh_job = g_scheduler.add_job(run_zgh_job, 'interval', seconds=1, args=[G_JOB_ID_4_ZGH, '1234'], id=G_JOB_ID_4_ZGH)
    ssh_job = g_scheduler.add_job(run_ssh_job, 'interval', seconds=2, args=[G_JOB_ID_4_SSH, '4321'], id=G_JOB_ID_4_SSH)
    g_scheduler.start()

    # pause
    time.sleep(20)
    # NOTE you can pause the certain job via any one as below :
    # zgh_job.pause()
    g_scheduler.get_job(job_id=G_JOB_ID_4_ZGH).pause()
    # scheduler.pause_job(job_id=G_JOB_ID_4_ZGH)
    # NOTE pause all jobs attached to this scheduler (zgh_job & ssh_job)
    # scheduler.pause()
    print('\npaused..............\n\n\n')

    time.sleep(20)
    # NOTE you can resume the certain job via any one as below :
    # zgh_job.resume()
    g_scheduler.get_job(job_id=G_JOB_ID_4_ZGH).resume()
    # scheduler.resume_job(job_id=G_JOB_ID_4_ZGH)
    # NOTE resume all jobs attached to this scheduler (zgh_job & ssh_job)
    # scheduler.resume()
    print('\nresumed..............\n\n\n')

    time.sleep(20)
    # NOTE you can stop the certain job as below :
    # zgh_job.remove()
    g_scheduler.get_job(job_id=G_JOB_ID_4_ZGH).remove()
    # scheduler.remove_job(job_id=G_JOB_ID_4_ZGH)
    # NOTE stop all jobs attached to this scheduler (zgh_job & ssh_job)
    # scheduler.shutdown(wait=False)
    print('\nstoped..............\n\n\n')


if __name__ == '__main__':
    print('hello world!\n\n\n\n', file=sys.stderr)

    main()

    while True:
        pass
